﻿using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Windows;

namespace WpfApp1.Models
{
    public class SharpBase : BindableBase
    {
        private double _left;
        public double Left { get => _left; set => SetProperty(ref _left, value); }

        private double _top;
        public double Top { get => _top; set => SetProperty(ref _top, value); }

        private double _width;
        public double Width { get => _width; set => SetProperty(ref _width, value); }

        private double _height;
        public double Height { get => _height; set => SetProperty(ref _height, value); }


        public ObservableCollection<FullyPort> Ports { get; set; } = new ObservableCollection<FullyPort>();

        public FullyPort TopPort => Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Top);
        public FullyPort BottomPort => Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Bottom);
        public FullyPort LeftPort => Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Left);
        public FullyPort RightPort => Ports?.FirstOrDefault(p => p.Orientation == PortOrientation.Right);

        public SharpBase()
        {
            Width = 100;
            Height = 100;

            Ports.Add(new FullyPort() { Orientation = PortOrientation.Left });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Top });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Right });
            Ports.Add(new FullyPort() { Orientation = PortOrientation.Bottom });

            this.PropertyChanged += SharpBase_PropertyChanged;
        }

        private double portWidth = 8;
        private double portHeight = 8;
        private void SharpBase_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Left) || e.PropertyName == nameof(Top) || e.PropertyName == nameof(Width) || e.PropertyName == nameof(Height))
            {
                Debug.WriteLine($"FullyPort_PropertyChanged--{DateTime.Now.ToString("yyyyMMdd-HHmmssfff")}");

                LeftPort.Position = new Point(Left, Top + Height / 2);
                TopPort.Position = new Point(Left + Width / 2, Top);
                RightPort.Position = new Point(Left + Width, Top + Height / 2);
                BottomPort.Position = new Point(Left + Width / 2, Top + Height);
            }
        }
    }
}
